import 'dart:async';

bool isAllowed = true;
Timer? throttleTimer;

// 扩展Function，添加防抖功能
extension DebounceExtension on Function {
  void Function() debounce([int milliseconds = 500]) {
    return () {
      if (throttleTimer?.isActive ?? false) throttleTimer?.cancel();
      throttleTimer = Timer(Duration(milliseconds: milliseconds), this());
    };
  }
}

// 扩展Function，添加节流功能
extension ThrottleExtension on Function {
  void Function() throttle([int milliseconds = 500]) {
    return () {
      if (!isAllowed) return;
      isAllowed = false;
      this();
      throttleTimer?.cancel();
      throttleTimer = Timer(Duration(milliseconds: milliseconds), () {
        isAllowed = true;
      });
    };
  }
}
